//
//  LspNbNetWorkManage+Extension.swift
//  LspSwiftFinayTest
//
//  Created by lsp's mac pro on 2018/4/16.
//  Copyright © 2018年 lsp's mac pro. All rights reserved.
//

import Foundation


extension LspNbNetWorkManage {
    
    

    /// - Parameters:
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博）默认为0
    ///   - max_id: 返回ID小于或等于max_id的微博 默认为0
    ///   - completion: 回调是成功否加载cheng g
    func statusList(since_id:Int64=0,max_id:Int64=0, completion:@escaping(_ list:[[String:AnyObject]]?,_ isSuccess:Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //int64不能转成anyobject 所以转换成字符串去操作
        let params=["since_id":"\(since_id)","max_id":"\(max_id>0 ? max_id-1 :0)"]
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
    
            let result = json?["statuses"] as? [[String:AnyObject]]
           print(json)
            completion(result, isSuccess)
        }
    }
    
    //获取未刷新微博多少条
    func getFreshCount( completion:@escaping (_ count:Int)->()) {
        
        guard let uid = userModel.uid else{
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remnid/unread_count.json"
        let params = ["uid" :uid]
        
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            print(json ?? "")
            
            let dict = json as? [String:AnyObject]
            let count = dict?["statues"] as? Int
            
            completion(count ?? 0)
            
            
        }
    }

}
extension LspNbNetWorkManage{
    
    func getUserToken(code:String,completion:@escaping (_ isSuccess:Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let param = ["client_id":WBAppkey,"client_secret":WBAppSercret,"grant_type":"authorization_code","code":code,"redirect_uri":WBReditrctURL]
        print("--------\(param)")
        
        request(Method: .POST, URLString: urlString, parameters: param as [String : AnyObject]) { (json, isSuccess) in
            print(json ?? "")
            //空字典的代号是[:]
            self.userModel.yy_modelSet(with: json as? [String:AnyObject] ?? [:])
           
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                
                self.userModel.yy_modelSet(with: dict)
                
                self.userModel.saveAccount()
                completion(true)

            })
        }
    }
}

extension LspNbNetWorkManage {
    
    //加载当前用户信息
    func loadUserInfo(completion:@escaping (_ dict:[String:AnyObject])->()) {
        
        guard let uid = userModel.uid else{
            return
        }

        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let param = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: param as [String : AnyObject]) { (json, isSuccess) in
            
            //完成回调
            completion((json as? [String:AnyObject]) ?? [:])
        }
        
    }
    
}

